﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace DevExpress.MailClient.Win.Controls.File_tabs.File_Viewers
{
    public partial class ucExcellFileViewer : DevExpress.XtraEditors.XtraUserControl
    {
        public ucExcellFileViewer()
        {
            InitializeComponent();
        }
        private string EXTENSION
        { get; set; }

        private string DestinationFilePath
        { get; set; }

        private string FILE_PATH
        {
            set
            {
                FileInfo docfileInfo = new FileInfo(value);
                //1. copy to a temp place
                DestinationFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), docfileInfo.Name + EXTENSION);
                if (!new FileInfo(DestinationFilePath).Exists)
                {
                    docfileInfo.CopyTo(DestinationFilePath, true);
                }
                {
                    this.spreadsheetControl1.LoadDocument(DestinationFilePath);
                }
            }
        }

        public ucExcellFileViewer(string filePath, string extention)
        {
            InitializeComponent();
            EXTENSION = extention;
            FILE_PATH = filePath;
        }


        private void bbiOpenMapFile_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            string surferPath = DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath;
            bool pathFound = false;

            if (string.IsNullOrEmpty(surferPath))
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "Select the surfer file.";
                ofd.Filter = "Executable Files | *.exe";
                if (ofd.ShowDialog().Equals(DialogResult.OK))
                {
                    DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath = ofd.FileName;
                    DevExpress.MailClient.Win.Properties.Settings.Default.Save();

                    surferPath = DevExpress.MailClient.Win.Properties.Settings.Default.SurferPath;

                    pathFound = true;


                }
            }
            else
            {
                pathFound = true;
            }
            if (pathFound)
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(surferPath);
                psi.RedirectStandardOutput = true;
                psi.Arguments = DestinationFilePath;
                // psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
                System.IO.StreamReader myOutput = proc.StandardOutput;
            }

        }
    }
}
